function formatUnit(num, fmt) {
        var res = "-";
        fmt = (fmt.length == 0) ? "" : fmt.toUpperCase().trim();
        if (isNaN(num)) num = 0;

        if ((num != 0 && fmt != "") || fmt == "ID") {

            switch (fmt) {
                case "ID":
                    num = parseInt(num).toString();
                    if (num.length < 4) {
                        var zero = new Array(5 - num.length).join("0");
                        num = zero + num;
                    }
                    res = num;
                    break;

                case "UN":
                case "PC":
                case "PCS":
                case "CTN":
                    res = parseInt(num);
                    break;

                case "$":
                case "CNY":
                case "BRL":
                case "USD":
                    num = parseFloat(num);
                    if (Math.abs(num) >= 0.001) {
                        res = (parseInt(Math.abs(num) * 1000) / 1000 - parseInt(Math.abs(num) * 100) / 100 != 0) ? formatNumeric(num, 3) : formatNumeric(num, 2);
                    }
                    break;

                case "M3":
                case "CBM":
                    num = parseFloat(num);
                    if (Math.abs(num) >= 0.001) {
                        if (num >= 100) {
                            res = formatNumeric(num, 1);
                        } else {
                            res = (Math.abs(num) < 1) ? formatNumeric(num, 3) : formatNumeric(num, 2);
                        }
                    }
                    break;

                case "KG":
                case "KGS":
                    num = parseFloat(num);
                    if (Math.abs(num) >= 0.01) {
                        res = formatNumeric(num, 2);
                    }
                    break;

                case "M":
                case "CM":
                    num = parseFloat(num);
                    if (Math.abs(num) >= 0.01) {
                        res = (num > 100) ? formatNumeric(num, 1) : formatNumeric(num, 2);
                    }
                    break;

                case "%":
                    num = parseFloat(num);
                    if (Math.abs(num) >= 0.01) {
                        if (num >= 100) {
                            res = (parseInt(Math.abs(num) * 10) / 10 == Math.abs(num)) ? parseInt(num) : formatNumeric(num, 1);
                        } else {
                            res = (Math.abs(num) < 10) ? formatNumeric(num, 2) : formatNumeric(num, 1);
                        }
                    }
                    break;
            }
        }
        return res;
    }
    function formatNumeric(num, digits) {
        if (isNaN(num)) num = 0;
        if (isNaN(digits = parseInt(digits))) digits = 0;
        var neg = (num < 0) ? "-" : "";
        num = Math.abs(num);
        var factor = Math.pow(10, digits);
        var res = Math.round(factor * num).toString();
        var decimal = "";
        if (digits > res.length) {
            decimal = new Array(digits - res.length + 1).join("0");
            decimal += (num < 1) ? res.substr(0, digits) : res.substr(res.length - digits);
        } else {
            decimal = res.substr(res.length - digits);
        }
        var plus = (res.substr(0, res.length - digits) == "") ? "0" : res.substr(0, res.length - digits);
        return neg + plus + '.' + decimal;
    }
